package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.enums.app.CheckStatus;
import com.yunkeji.common.exception.ServiceException;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.LikeWall;
import com.yunkeji.dao.mapper.LikeWallMapper;
import com.yunkeji.dao.vo.LikeWallVo;
import com.yunkeji.system.domain.bo.CheckBo;
import com.yunkeji.system.domain.bo.LikeWallBo;
import com.yunkeji.system.service.ILikeWallService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 贴贴墙Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class LikeWallServiceImpl implements ILikeWallService {

    private final LikeWallMapper baseMapper;

    /** 查询贴贴墙 */
    @Override
    public LikeWallVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询贴贴墙列表 */
    @Override
    public TableDataInfo<LikeWallVo> queryPageList(LikeWallBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LikeWall> lqw = buildQueryWrapper(bo);
        Page<LikeWallVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询贴贴墙列表 */
    @Override
    public List<LikeWallVo> queryList(LikeWallBo bo) {
        LambdaQueryWrapper<LikeWall> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LikeWall> buildQueryWrapper(LikeWallBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LikeWall> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, LikeWall::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), LikeWall::getContent, bo.getContent());
        lqw.eq(bo.getType() != null, LikeWall::getType, bo.getType());
        lqw.eq(bo.getNums() != null, LikeWall::getNums, bo.getNums());
        lqw.eq(bo.getStatus() != null, LikeWall::getStatus, bo.getStatus());
        lqw.eq(bo.getSex() != null, LikeWall::getSex, bo.getSex());
        lqw.eq(bo.getAge() != null, LikeWall::getAge, bo.getAge());
        lqw.eq(bo.getHeight() != null, LikeWall::getHeight, bo.getHeight());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadInfo()), LikeWall::getHeadInfo, bo.getHeadInfo());
        lqw.eq(bo.getProfession() != null, LikeWall::getProfession, bo.getProfession());
        lqw.like(StringUtils.isNotBlank(bo.getCityName()), LikeWall::getCityName, bo.getCityName());
        lqw.eq(
                StringUtils.isNotBlank(bo.getPubHeadUrl()),
                LikeWall::getPubHeadUrl,
                bo.getPubHeadUrl());
        lqw.orderByDesc(LikeWall::getCreateTime);
        return lqw;
    }

    /** 新增贴贴墙 */
    @Override
    public Boolean insertByBo(LikeWallBo bo) {
        LikeWall add = BeanUtil.toBean(bo, LikeWall.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改贴贴墙 */
    @Override
    public Boolean updateByBo(LikeWallBo bo) {
        LikeWall update = BeanUtil.toBean(bo, LikeWall.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(LikeWall entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除贴贴墙 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void pass(CheckBo bo) {
        LikeWall likeWall = baseMapper.selectById(bo.getId());
        if (!likeWall.getStatus().equals(CheckStatus.待审核.getCode())) {
            throw new ServiceException("操作失败");
        }
        likeWall.setStatus(CheckStatus.审核通过.getCode());
        baseMapper.updateById(likeWall);
    }

    @Override
    public void refuse(CheckBo bo) {
        LikeWall likeWall = baseMapper.selectById(bo.getId());
        if (!likeWall.getStatus().equals(CheckStatus.待审核.getCode())) {
            throw new ServiceException("操作失败");
        }
        likeWall.setStatus(CheckStatus.审核拒绝.getCode());
        baseMapper.updateById(likeWall);
    }
}
